<template>
  <div>
    <div class="bread">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '#' }" style="font-size: 20px;">首页</el-breadcrumb-item>
        <el-breadcrumb-item style="font-size: 20px;">类别管理</el-breadcrumb-item>
        <el-breadcrumb-item style="font-size: 20px;">添加类别</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div style="width: 400px; margin-top: 50px;">
      <el-form :model="bookTypeForm" :rules="rules" ref="bookTypeForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="类别名字" prop="typeName">
          <el-input v-model="bookTypeForm.typeName"></el-input>
        </el-form-item>
        <el-form-item label="父类别" prop="parentId">
          <el-cascader :options="bookTypes" :props="props" v-model="bookTypeForm.parentId" style="margin-left: -90px;" clearable></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button style="margin-left: -90px;" type="primary" @click="submitForm('bookTypeForm')">立即添加</el-button>
          <el-button @click="resetForm('bookTypeForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        bookTypeForm: {
          typeName: '',
          parentId: ''
        },
        rules: {
          typeName: [{
            required: true,
            message: '请输入类别名',
            trigger: 'blur'
          }],
          parentId: [{
            required: true,
            message: '请选择父类别',
            trigger: 'change'
          }]
        },
        cascaderProps: {
          value: 'typeId',
          label: 'typeName',
          children: 'children',
        },
        bookTypes:[],
        props:{
          expandTrigger: "hover",
          checkStrictly: true,
          value: 'typeId',
          label: 'typeName',
          children: 'children',
          leaf:false
        }
      };
    },
    methods: {
      submitForm(bookTypeForm) {
        this.$refs[bookTypeForm].validate((valid) => {
          if (valid) {
            let num = this.bookTypeForm.parentId.length;
            this.bookTypeForm.parentId=this.bookTypeForm.parentId[num-1]
            console.log(this.bookTypeForm)
            this.$http.post("/bookType/admin/addBookType",this.bookTypeForm).then(resp=>{
              alert(resp.data.message)
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(bookTypeForm) {
        this.$refs[bookTypeForm].resetFields();
      },
      listAllBookTypes(){
        this.$http.get("/bookType/admin/selectBookTypeTree?typeState="+0).then(resp=>{
          this.bookTypes=resp.data.data
          console.log(resp.data.data)
        })
      }
    },
    created() {
      this.listAllBookTypes();
    }
  }
</script>

<style scoped>
  .bread {
    margin-top: 50px;
    margin-left: 50px;
  }
</style>

<style>
  .el-input__inner::placeholder {
    color: #31E5F5;
  }
</style>